package com.huo.thread.semaphoredemo;

import java.util.concurrent.Semaphore;

public class Demo {
    public void method(Semaphore semaphore){
        try {
            semaphore.acquire();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"执行...");
        semaphore.release();
    }

    public static void main(String[] args) {
        Demo d = new Demo();
        Semaphore semaphore = new Semaphore(5);
        for(int i = 0 ; i < 30 ; i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    d.method(semaphore);
                }
            }).start();
        }
    }
}
